﻿package org.springframework.samples.mvc.mapping;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class MappingController {

	protected final Log logger = LogFactory.getLog(getClass());
	/**
	 * 处理请求路径为"/mapping/path"的请求
	 */
	@RequestMapping("/mapping/path")
	public @ResponseBody String byPath() {
		return "Mapped by path!";
	}
	/**
	 * 处理请求路径为"/mapping/path/[任意路径]"的GET请求
	 */
	@RequestMapping(value="/mapping/path/*", method=RequestMethod.GET)
	public @ResponseBody String byPathPattern(HttpServletRequest request) {
		return "Mapped by path pattern ('" + request.getRequestURI() + "')";
	}
	/**
	 * 处理请求路径为"/mapping/method"的GET请求
	 */
	@RequestMapping(value="/mapping/method", method=RequestMethod.GET)
	public @ResponseBody String byMethod() {
		return "Mapped by path + method";
	}
	/**
	 * 处理请求路径为"/mapping/parameter"的GET请求,并且要求请求中必须包含parameter名称为"foo",否则将不处理该请求
	 * （还可以params="myParam=myValue"这样使用，表示要求请求中必须包含parameter名称为"myParam"、parameter值为"myValue"的参数）
	 */
	@RequestMapping(value="/mapping/parameter", method=RequestMethod.GET, params="foo")
	public @ResponseBody String byParameter() {
		return "Mapped by path + method + presence of query parameter!";
	}
	/**
	 * 处理请求路径为"/mapping/parameter"的GET请求,并且要求请求中必须不能包含parameter名称为"foo"的参数,否则将不处理该请求
	 * （还可以params="myParam=myValue"这样使用，表示要求请求中必须包含parameter名称为"myParam"、parameter值为"myValue"的参数）
	 */
	@RequestMapping(value="/mapping/parameter", method=RequestMethod.GET, params="!foo")
	public @ResponseBody String byParameterNegation() {
		return "Mapped by path + method + not presence of query parameter!";
	}
	/**
	 * 处理请求路径为"/mapping/header"的GET请求,并且要求请求的header中必须包含"FooHeader=foo"，否则将不处理该请求
	 */
	@RequestMapping(value="/mapping/header", method=RequestMethod.GET, headers="FooHeader=foo")
	public @ResponseBody String byHeader() {
		return "Mapped by path + method + presence of header!";
	}
	/**
	 * 处理请求路径为"/mapping/header"的GET请求,并且要求请求的header中必须不包含"FooHeader"，否则将不处理该请求
	 */
	@RequestMapping(value="/mapping/header", method=RequestMethod.GET, headers="!FooHeader")
	public @ResponseBody String byHeaderNegation() {
		return "Mapped by path + method + absence of header!";
	}
	/**
	 * 依赖Jackson2的三个jar包，否则报错HTTP Status 416<br>
	 * 处理请求路径为"/mapping/consumes"的POST请求，并且必须是（请求的Content-Type类型）与 （consumes指定的内容） 匹配的请求，否则将不处理该请求<br>
	 */
	@RequestMapping(value="/mapping/consumes", method=RequestMethod.POST, consumes=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody String byConsumes(@RequestBody JavaBean javaBean) { 
		logger.info("===>" + javaBean);
		return "Mapped by path + method + consumable media type (javaBean '" + javaBean + "')";
	}
	/**
	 * 依赖Jackson2的三个jar包，否则报错HTTP Status 406<br>
	 * 处理请求路径为"/mapping/produces"的GET请求，并且必须是（请求的Accept的内容）与 （produces指定的内容） 匹配的请求，否则将不处理该请求<br>
	 * 或者是处理请求路径后缀为.json的请求（前提是FrontController接收这样后缀的请求，请查看web.xml确认）
	 */
	@RequestMapping(value="/mapping/produces", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody JavaBean byProducesJson() {
		logger.info("===>byProducesJson");
		return new JavaBean();
	}
	/**
	 * 处理请求路径为"/mapping/produces"的GET请求，并且必须是（请求的Accept的内容）与 （produces指定的内容） 匹配的请求，否则将不处理该请求<br>
	 * 或者是处理请求路径后缀为.xml的请求（前提是FrontController接收这样后缀的请求，请查看web.xml确认）<br>
	 * 
	 * JavaBean必须采用“@XmlRootElement”注解，否则报错HTTP Status 406
	 */
	@RequestMapping(value="/mapping/produces", method=RequestMethod.GET, produces=MediaType.APPLICATION_XML_VALUE)
	public @ResponseBody JavaBean byProducesXml() {
		logger.info("===>byProducesXml");
		return new JavaBean();
	}

}
